package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目类型工厂
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {

    //四个实现类
    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    //SubjectInfoTypeEnum枚举的某个对象如 RADIO(1, "单选")
    private Map<SubjectInfoTypeEnum,SubjectTypeHandler> handlerMap = new HashMap<>();

    //工厂提供实现类出去，从Map中拿SubjectTypeHandler
    public SubjectTypeHandler getHandler(int subjectType){
        //枚举中的静态方法获取枚举对象如 RADIO(1, "单选")
        SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
        return handlerMap.get(subjectInfoTypeEnum);
    }

    //handlerMap是空的，bean加载完后执行的方法
    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlerList) {
            //subjectTypeHandler.getHandlerType()枚举的某个常量 subjectTypeHandler对应的实现类
            handlerMap.put(subjectTypeHandler.getHandlerType(),subjectTypeHandler);
        }
    }
}
